package cn.lsnu.modules.oa.service;

import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.interfacetools.interfaceservice.BaseService;
import cn.lsnu.modules.login.entity.LoginEntity;
import cn.lsnu.modules.oa.entity.RoleEntity;
import cn.lsnu.modules.oa.entity.UserEntity;
import cn.lsnu.modules.oa.mapper.UserMapper;
import cn.lsnu.utils.Guuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends BaseService<UserEntity,UserMapper> {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void save(UserEntity entity) throws Exception {
        entity.setUserId(Guuid.uuid());
        entity.setUserStatus(entity.getUserStatus() == null?1:entity.getUserStatus());
        //插入user-role关联的数据
        insertUR(entity);
        super.save(entity);
    }

    @Override
    public UserEntity get(String key) throws Exception {
        return super.get(key);
    }

    @Override
    public UserEntity getEntity(UserEntity entity) throws Exception {
        return super.getEntity(entity);
    }

    @Override
    public List<UserEntity> findList(UserEntity entity) throws Exception {
        return super.findList(entity);
    }

    @Override
    public Page<UserEntity> findPage(UserEntity entity) throws Exception {
        Page<UserEntity> lists = super.findPage(entity);
        List<UserEntity> roleLists = lists.getData();
        roleLists = lists.getData();
        for (int i=0;i<roleLists.size();i++){
            String roleName = "";
            boolean ex=roleLists.get(i).getRoles().isEmpty();
            if(roleLists.get(i).getRoles() == null ){
                return lists;
            }
            int inroleS = roleLists.get(i).getRoles().size();
            if(inroleS != 0){
                for(int j=0;j<inroleS;j++){
                    roleName =roleName + roleLists.get(i).getRoles().get(j).getRoleName()+" ";
                }
            }
            roleLists.get(i).setRoleName(roleName);
        }
        return lists;
    }

    @Override
    public void delete(UserEntity entity) throws Exception {
        userMapper.deleteUR(entity);
        super.delete(entity);
    }

    @Override
    public void update(UserEntity entity) throws Exception {
        //先删除在插入（修改）
        userMapper.deleteUR(entity);
        insertUR(entity);
        super.update(entity);
    }

    public void insertUR(UserEntity entity)throws Exception{
        if(entity.getRoles() != null){
            for(int i=0;i<entity.getRoles().size();i++){
                if(entity.getRoles().get(i).getRoleId() != null){
                    UserEntity urole = new UserEntity();
                    urole.setUserId(entity.getUserId());
                    urole.setRoleId(entity.getRoles().get(i).getRoleId());
                    userMapper.saveUR(urole);
                }
            }
        }

    }

    public void updateStatus(UserEntity entity)throws Exception{
        userMapper.updateStatus(entity);
    }

}
